﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace SpeedTestServerSetup
{
	public enum WizardDirection
	{
		Next,
		Previous,
		Stay,
		Unknown
	}

	public partial class InstallationWizard : Form
	{
		WizardPage[] wizPages = new WizardPage[6];
		int currentPage = 0;

		public InstallationWizard()
		{
			InitializeComponent();
		}

		private void InstallationWizard_Load(object sender, EventArgs e)
		{
			LoadWizardPages();
			LoadPage(WizardDirection.Stay);

			if (!ConfigurationManager.AppSettings.Get("FirstRun").Equals("true"))
			{
				DialogResult res = MessageBox.Show(this, "This wizard appears to have been run before. Resume installation?", "Wizard", MessageBoxButtons.YesNo);
				if (res == DialogResult.Yes)
				{
					currentPage = Int32.Parse(ConfigurationManager.AppSettings.Get("ResumePage"));
					LoadPage(WizardDirection.Stay);
				}
			}
		}

		private void LoadWizardPages()
		{
			wizPages[0] = new Page1();
            wizPages[0].ReadyStateChanged += new StateChangeEvent(WizardPage_ReadyStateChanged);
			wizPages[1] = new Page2();
            wizPages[1].ReadyStateChanged += new StateChangeEvent(WizardPage_ReadyStateChanged);
			wizPages[2] = new Page3();
            wizPages[2].ReadyStateChanged += new StateChangeEvent(WizardPage_ReadyStateChanged);
			wizPages[3] = new Page4();
            wizPages[3].ReadyStateChanged += new StateChangeEvent(WizardPage_ReadyStateChanged);
			wizPages[4] = new Page5();
			wizPages[4].ReadyStateChanged += new StateChangeEvent(WizardPage_ReadyStateChanged);
			wizPages[5] = new Page6();
            wizPages[5].ReadyStateChanged += new StateChangeEvent(WizardPage_ReadyStateChanged);
		}

        void WizardPage_ReadyStateChanged(bool isReady)
        {
            btnNext.Enabled = isReady;
        }

		private void LoadPage(WizardDirection direction)
		{
            WizardEvents e = new WizardEvents();
			e.Direction = direction;
            wizPages[currentPage].LeavePage(e);

            if (e.Cancel)
            {
                MessageBox.Show(this, e.ErrorMessage);
            }
            else
            {
                if (currentPage > 0 && direction == WizardDirection.Previous) { currentPage--; }

                if (currentPage < wizPages.Length - 1 && direction == WizardDirection.Next) { currentPage++; }

                this.panelMain.Controls.Clear();
                panelMain.Controls.Add(wizPages[currentPage]);
                wizPages[currentPage].EnterPage(e);
            }

			
		}

		private void btnNext_Click(object sender, EventArgs e)
		{
			if (btnNext.Text != "Finish")
			{
                if (currentPage == wizPages.Length - 2) //after page 5, index 4, change to finish
                {
                    btnNext.Text = "Finish";
                }
                else
                {
                    btnNext.Enabled = false;    //disable it. It get enabled by the pages.
                }

				LoadPage(WizardDirection.Next);

				if (currentPage > 0) { btnPrevious.Enabled = true; }
                
			}
			else
			{
				this.Close();
			}
		}

		private void btnPrevious_Click(object sender, EventArgs e)
		{
			LoadPage(WizardDirection.Previous);
			if (currentPage == 0){ 	btnPrevious.Enabled = false; }
			if (currentPage != wizPages.Length - 1) { btnNext.Text = "Next"; }	//reset in case the most recent page was the last page

		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

        private void btnHelp_Click(object sender, EventArgs e)
        {
            Help.OpenHtmlHelp(currentPage);
        }

	}
}
